package de.hsma.srn.view.console;

import java.util.Scanner;

import de.hsma.srn.controller.KeyBoxController;
import de.hsma.srn.interfaces.IListener;
import de.hsma.srn.model.account.DomainModel;

public class KeyBoxConsole implements IListener, IConsole {
	private KeyBoxController mKeyBoxController;
	
	public KeyBoxConsole(KeyBoxController keyBoxController) {
		this.mKeyBoxController = keyBoxController;
	}
	
	public void showDialog() {
		int wahl;
		Scanner scanner = new Scanner(System.in);
		do {
			showTitle();
			showKeyBox();
			showMenuOptions();		
			
			String input = scanner.next();
			try {
				wahl = Integer.parseInt(input);
			} catch(Exception e) {
				wahl = 0;
			}
			String filename;
			switch(wahl) {
			case 1:
				showTitle();
				System.out.println("[Moegliche Dateien in '" + this.mKeyBoxController.getHomePath() + "']");
				for(String file : this.mKeyBoxController.getFileList()) {
					System.out.println(file);
				}
				System.out.println("Bitte geben sie einen Dateinamen an:");
				filename = scanner.next();
				this.mKeyBoxController.createKey(this.mKeyBoxController.getHomePath() + "/" + filename);
				break;
			case 2:
				showTitle();
				showKeyBox();
				System.out.println("Bitte geben sie einen oben genannten Dateipfad an:");
				filename = scanner.next();
				this.mKeyBoxController.useKey(filename);
				break;
			default:
				this.mKeyBoxController.logout();
				break;
			}
		} while(wahl!=0);
	}
	
	private void showTitle() {
		System.out.println("\n\n\n\n\n\n\n\n\n");
		System.out.println("[Keybox von " + this.mKeyBoxController.getUsername() + "]");
	}
	private void showKeyBox() {
		System.out.println("---------------------------------");
		System.out.println("[Meine Schluessel:]");
		String[] ownKeys = this.mKeyBoxController.getOwnKeys();
		for(String key : ownKeys) {
			System.out.println(key);
		}
		
		System.out.println("---------------------------------");
		System.out.println("[Fremde Schluessel:]");
		String[] assignedKeys = this.mKeyBoxController.getAssignedKeys();
		for(String key : assignedKeys) {
			System.out.println(key);
		}
		System.out.println("---------------------------------");
	}
	private void showMenuOptions() {
		System.out.println("");
		System.out.println("");
		System.out.println("Was soll ich tun?");
		System.out.println(" 1.) Schluessel erstellen");
		System.out.println(" 2.) Schluessel benutzen");
		System.out.println(" xyz.) Schnauze halten und ausloggen\n");
	}
	 
	@Override
	public void update(DomainModel domain) {
		showDialog();
	}
}
